<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <div th:replace="header::common_resource"></div>
</head>
<body>

    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">类别名字</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">父级分类</label>
            <div class="layui-input-inline">
                <select id="topCategory"  name="parentId" lay-filter="topFilter">
                    <option value="0">顶级分类</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" th:title="正常" checked/>
                <input type="radio" name="status" value="0" th:title="失效"/>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" onclick="submitForm()" class="layui-btn" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script th:inline="javascript">

        layui.use(['form'], function () {
            var form = layui.form;
            $.post(
                [[${#request.getContextPath()}]]+'/category/selectAllTop',
                function (result) {
                    // console.log(result);
                    $(result).each(function () {
                        $('#topCategory').append('<option value="'+this.id+'">'+this.name+'</option>');
                        layui.form.render('select');
                    });
                },
                'json'
            );
        });

        function submitForm() {
            $.post(
                [[${#request.getContextPath()}]]+'/category/add',
                $('#formId').serialize(),
                function(result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg)
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>